﻿@{
    Layout = "~/Views/Shared/OA/_Tree.cshtml";
}
@section Toolbar
{
    @Html.MyNet().Button(new ButtonOptions { Text = "新建", Icon = "create" }, new[] 
    {
        new ButtonOptions { Text = "新建分类", Click = "create(1)", Icon = "create"  },
        new ButtonOptions { Text = "新建应用", Click = "create(2)", Icon = "create"  },
        new ButtonOptions { Text = "新建权限", Click = "create(3)", Icon = "create"  }
    })
    @Html.MyNet().Button(new ButtonOptions { Text = "修改", Click = "modify" })
    @Html.MyNet().Button(new ButtonOptions { Text = "复制", Click = "copy" })
    @Html.MyNet().Button(new ButtonOptions { Text = "删除", Click = "removee", Icon = "remove" })
}
@section Tree
{
    <div id="tree" class="tree">
    </div>
}
@section Main
{
    <div id="detail">
    </div>
}
<script type="text/javascript">

    var url = "/Common/Module/";

    jQuery(function ()
    {
        $("#tree").kendoTreeView(
        {
            dataSource:
            {
                transport:
                {
                    read: { url: url + "Read" }
                },
                schema:
                {
                    data: "Data",
                    model: { children: "childs" },
                    errors: "Errors"
                },
                error: function (e)
                {
                    alert(e.errors);
                }
            },
            dragAndDrop: true,
            dataBound: onDataBound,
            select: onSelect,
            dragend: onDragend
        });
    });

    function create(type)
    {
        if (treeSelected())
        {
            nav(url + "Create?ParentId=" + treeSelected() + "&Type=" + type);
        }
        else
        {
            nav(url + "Create?Type=" + type);
        }
    }

    function modify()
    {
        if (!treeSelected()) return alert("未选中任何节点！");
        nav(url + "Modify?Id=" + treeSelected());
    }

    function removee()
    {
        if (!treeSelected()) return alert("未选中任何节点！");
        if (!confirm("确定要删除选中的数据吗？")) return;

        $.ajax
        ({
            url: url + "Remove",
            data: { id: treeSelected() },
            type: "post",
            error: disposeError,
            success: function (result)
            {
                if (disposeException(result)) return;
                reload(treeSelected());
            }
        });
    }

    function move(sourceId, destinationId, position)
    {
        $.ajax
        ({
            url: url + "Move",
            data: { sourceId: sourceId, destinationId: destinationId, position: position },
            type: "post",
            error: disposeError,
            success: function (result)
            {
                if (disposeException(result)) return;
            }
        });
    }

    function copy()
    {
        if (!treeSelected()) return alert("未选中任何节点！");

        $.ajax
        ({
            url: url + "Copy",
            data: { id: treeSelected() },
            type: "post",
            error: disposeError,
            success: function (result)
            {
                if (disposeException(result)) return;
                reload();
            }
        });
    }

    function detail()
    {
        $.ajax
        ({
            url: url + "Detail",
            data: { id: treeSelected() },
            type: "post",
            error: disposeError,
            success: function (result)
            {
                if (disposeException(result)) return;
                bind(result);
            }
        });
    }

</script>
